package xyz.renyumeng.xkm.shop.service.impl;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.renyumeng.xkm.client.user.UserInfoFeignClient;
import xyz.renyumeng.xkm.exception.XkmException;
import xyz.renyumeng.xkm.model.es.EsProduct;
import xyz.renyumeng.xkm.model.shop.ProductInfo;
import xyz.renyumeng.xkm.model.shop.ProductTypeInfo;
import xyz.renyumeng.xkm.model.user.UserInfo;
import xyz.renyumeng.xkm.shop.mapper.ProductMapper;
import xyz.renyumeng.xkm.shop.service.ProductService;
import xyz.renyumeng.xkm.shop.service.ProductTypeInfoService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author renyumeng
 * @date Created in 16:16 2023/6/30
 */

@Service
@Transactional
public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductInfo> implements ProductService {

    @Resource
    private UserInfoFeignClient userInfoFeignClient;

    @Resource
    private ProductTypeInfoService productTypeInfoService;

    @Override
    public ProductInfo getWithProDuct4OwnerDesc(Long id) {
        // 查询商品信息
        LambdaQueryWrapper<ProductInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(ProductInfo::getId, id)
                .eq(ProductInfo::getIsSoled, 0)
                .eq(ProductInfo::getIsListed, 0);
        ProductInfo productInfo = this.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(productInfo)) {
            throw new XkmException(201, "商品信息不存在!");
        }
        Long ownerId = productInfo.getOwnerId();
        Long typeId = productInfo.getTypeId();

        // 远程调用userInfo服务获取用户信息
        UserInfo userInfo = userInfoFeignClient.innerGetUserByIdWithAddressInfo(ownerId);

        // 获取type信息
        ProductTypeInfo productTypeInfo = productTypeInfoService.getById(typeId);
        if (ObjectUtil.isEmpty(productTypeInfo)) {
            throw new XkmException(201, "类别不存在!");
        }

        // 添加到param
        productInfo.getParam().put("username", userInfo.getUsername());
        productInfo.getParam().put("gender", userInfo.getGender());
        productInfo.getParam().put("desc", userInfo.getDesc());
        productInfo.getParam().put("creditScore", userInfo.getCreditScore());
        productInfo.getParam().put("type", productTypeInfo.getType());
        return productInfo;
    }

    @Override
    public Boolean innerUpdateSoledInfoById(Long id) {
        LambdaUpdateWrapper<ProductInfo> productInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        productInfoLambdaUpdateWrapper
                .eq(ProductInfo::getId, id)
                .set(ProductInfo::getIsSoled, 1);
        boolean update = this.update(productInfoLambdaUpdateWrapper);
        return BooleanUtil.isTrue(update);
    }

    @Override
    public List<EsProduct> getAll() {
        return baseMapper.getAllEsProductList(null);
    }
}
